package com.sgb.kjwl.view.ui.activity.leaseAsk.publish;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.sdk.mobile.manager.login.NoDoubleClickListener;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.databinding.ActivityPublishSuccessBinding;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PublishSuccessActivity extends AppActivity {
    private ActivityPublishSuccessBinding binding;
    private String id;
    private String categoryType;

    public static void start(Context context, String id, String categoryType) {
        Intent intent = new Intent(context, PublishSuccessActivity.class);
        intent.putExtra("id", id);
        intent.putExtra("categoryType", categoryType);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_publish_success);
        binding.ivBack.setOnClickListener(v -> finish());
        binding.tvOk.setOnClickListener(v -> finish());
        binding.tvGoto.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                finish();
                if ("1".equals(categoryType)) {//机械
                    PublishLeaseInfoActivity2.start(getActivity(), 0);
                } else if ("2".equals(categoryType)) {//设备
                    PublishLeaseInfoActivity2.start(getActivity(), 1);
                } else if ("3".equals(categoryType)) {//车辆
                    PublishLeaseInfoActivity2.start(getActivity(), 2);
                } else if ("4".equals(categoryType)) {//房产
                    AskLeaseHouseActivity2.start(getActivity());
                } else if ("5".equals(categoryType)) {//土地
                    AskLeaseLandActivity2.start(getActivity());
                } else if ("6".equals(categoryType)) {//工厂
                    AskLeaseFactoryActivity2.start(getActivity());
                } else if ("7".equals(categoryType)) {//农机
                    AskLeaseFarmActivity2.start(getActivity());
                }
            }
        });
        binding.tvDetail.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                finish();
                getH5Url(id, categoryType);
            }
        });
    }

    @Override
    protected void init() {
        if (getIntent() != null) {
            id = getIntent().getStringExtra("id");
            categoryType = getIntent().getStringExtra("categoryType");
        }
    }

    /**
     * 求租详情
     *
     * @param categoryType
     * @param id
     */
    public void getH5Url(String id, String categoryType) {
        String urlName = "";
        if ("1".equals(categoryType)) {//机械
            urlName = "mechanicsoffer";
        } else if ("2".equals(categoryType)) {//设备
            urlName = "equipmentoffer";
        } else if ("3".equals(categoryType)) {//车辆
            urlName = "vehicleoffer";
        } else if ("4".equals(categoryType)) {//房产
            urlName = "houseoffer";
        } else if ("5".equals(categoryType)) {//土地
            urlName = "landOfferDetails";
        } else if ("6".equals(categoryType)) {//工厂
            urlName = "factoryOfferDetails";
        } else if ("7".equals(categoryType)) {//农机
            urlName = "farmOfferDetails";
        }
        DialogHelper.showProgressDialog(this, "数据加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting(urlName).enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?id=" + id;
//                    String url = "http://192.168.2.103:8068/personCenterMechanicalDetail" + "?id=" + id;
                    WebViewX5HostActivity.start(PublishSuccessActivity.this, url);
                } else {
                    MToast.showToast(PublishSuccessActivity.this, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

}
